﻿using GPS.COMMON;
using GPS.DataAccessLayer;
using GPS.GPS_DAL;
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;

namespace GPS.Models
{
    //public class ExtentionMail
    //{
    //    public static IEnumerable<TSource> DistinctBy<TSource, TKey>
    //   (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
    //    {
    //        HashSet<TKey> seenKeys = new HashSet<TKey>();
    //        foreach (TSource element in source)
    //        {
    //            if (seenKeys.Add(keySelector(element)))
    //            {
    //                yield return element;
    //            }
    //        }
    //    }
    //}
    public class JobEmailclass : IJob
    {

        public void Execute(IJobExecutionContext context)
        {
            DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;

            var _lstEmail = _db.GetAll<DDSCANHBAO>();

            Dictionary<string, int> dictDataPT = new Dictionary<string, int>();
            Dictionary<string, int> dictDataCSDT = new Dictionary<string, int>();
            Dictionary<string, int> dictDataDUPGV = new Dictionary<string, int>();
            Dictionary<string, int> dictDataDUPPT = new Dictionary<string, int>();
            if (_lstEmail != null && _lstEmail.Count > 0)
            {
                List<string> lstGTVT = _lstEmail.Select(p => p.DSCANHBAO_DVGTVT_MA).ToList();
                lstGTVT = lstGTVT.Distinct().ToList();
                for (int i = 0; i < lstGTVT.Count; i++)
                {
                    string _maGTVT = lstGTVT[i];
                    var lstPT = Singleton<GPS_CanhBaoDAO>.Inst.GPS_CANHBAO_PT(_maGTVT);
                    if (lstPT != null && lstPT.Count > 0)
                    {
                        dictDataPT.Add(_maGTVT, lstPT.Count);
                    }
                    var lstCSDT = Singleton<GPS_CanhBaoDAO>.Inst.GPS_CANHBAO_CSDT(_maGTVT);
                    if (lstCSDT != null && lstCSDT.Count > 0)
                    {
                        dictDataCSDT.Add(_maGTVT, lstCSDT.Count);
                    }
                }

                //ViewBag.GPS_CANHBAO_CSDTs = Singleton<GPS_CanhBaoDAO>.Inst.GPS_CANHBAO_CSDT(_maGTVT);
                //ViewBag.GPS_CANHBAO_GV_DUP_CMTs = Singleton<GPS_CanhBaoDAO>.Inst.GPS_CANHBAO_GV_DUP_CMT();
                //ViewBag.GPS_CANHBAO_PT_DUP_BIENSOs = Singleton<GPS_CanhBaoDAO>.Inst.GPS_CANHBAO_PT_DUP_BIENSO();
                var lstDUPGV = Singleton<GPS_CanhBaoDAO>.Inst.GPS_CANHBAO_GV_DUP_CMT();
                if (lstDUPGV != null && lstDUPGV.Count > 0)
                {
                    dictDataDUPGV.Add("GPS_CANHBAO_GV_DUP_CMT", lstDUPGV.Count);
                }
                var lstDUPPT = Singleton<GPS_CanhBaoDAO>.Inst.GPS_CANHBAO_PT_DUP_BIENSO();
                if (lstDUPPT != null && lstDUPPT.Count > 0)
                {
                    dictDataDUPPT.Add("GPS_CANHBAO_PT_DUP_BIENSO", lstDUPPT.Count);
                }

                //guier mail
                string emailFrom = System.Configuration.ConfigurationManager.AppSettings["Email"];
                string passFrom = System.Configuration.ConfigurationManager.AppSettings["Password"];

                for (int i = 0; i < _lstEmail.Count; i++)
                {
                    string _emailTo = _lstEmail[i].DSCANHBAO_MAIL;
                    StringBuilder strBuilder = new StringBuilder();
                    if (dictDataPT.Count > 0 && dictDataPT.ContainsKey(_lstEmail[i].DSCANHBAO_DVGTVT_MA))
                        strBuilder.AppendLine("<b style=\"color: #b94a48;font-size: 14px;\"> Cảnh báo phương tiện hết hạn giấy phép:" + dictDataPT[_lstEmail[i].DSCANHBAO_DVGTVT_MA] + "</b> <br/>");

                    if (dictDataCSDT.Count > 0 && dictDataCSDT.ContainsKey(_lstEmail[i].DSCANHBAO_DVGTVT_MA))
                        strBuilder.AppendLine("<b style=\"color: #b94a48;font-size: 14px;\"> Cảnh báo cơ sở đào tạo hết hạn giấy phép:" + dictDataCSDT[_lstEmail[i].DSCANHBAO_DVGTVT_MA] + "</b> <br/>");

                    if (dictDataDUPGV.Count > 0)
                        strBuilder.AppendLine("<b style=\"color: #b94a48;font-size: 14px;\"> Cảnh báo giáo viên trùng chứng minh thư:" + dictDataDUPGV["GPS_CANHBAO_GV_DUP_CMT"] + "</b> <br/>");

                    if (dictDataDUPPT.Count > 0)
                        strBuilder.AppendLine("<b style=\"color: #b94a48;font-size: 14px;\"> Cảnh báo phương tiện trùng biển số:" + dictDataDUPPT["GPS_CANHBAO_PT_DUP_BIENSO"] + "</b> <br/>");

                    using (var message = new MailMessage(emailFrom, _emailTo))
                    {
                        message.IsBodyHtml = true;
                        message.Subject = "v.v GPS thông tin Cảnh báo ngày " + DateTime.Now.ToString();
                        message.Body = strBuilder.ToString();
                        using (SmtpClient client = new SmtpClient
                        {
                            EnableSsl = true,
                            Host = "smtp.gmail.com",
                            Port = 587,
                            Credentials = new NetworkCredential(emailFrom, passFrom)
                        })
                        {
                            client.Send(message);
                        }
                    }
                }

            }


        }
    }
    public class JobScheduler
    {
        public static void Start()
        {
            DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
            var _lstEmail = _db.GetOne<DDSCANHBAO>(o=>o.ID==1);
            var _songay = _lstEmail.DSCANHBAO_SONGAY.ToString();
            IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
            scheduler.Start();

            IJobDetail job = JobBuilder.Create<JobEmailclass>().Build();

            ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity("trigger1", "group1")
            .StartNow()
            .WithSimpleSchedule(x => x
            .WithIntervalInSeconds(Convert.ToInt32(_songay))
            .RepeatForever())
            .Build();

            scheduler.ScheduleJob(job, trigger);
        }
    }
}